package com.xbniao.owms.utils.barcode;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.xbniao.commons.service.exception.SOAServiceException;
/**
  * 
  * @ClassName: BarCodeUtil
  * @author zhouliwei@xbniao.com
  * @date 2015年9月9日 下午5:09:34
  */
public class BarCodeUtil {
	private static  List<BarCodePDFTemplate> barCodeTemplate = new ArrayList<>();
	
	private static  List<BarCodePDFTemplate> commodityBarCodeTemplate = new ArrayList<>();
	
	public static Map<String,CommodityBarCodeParamByTemplateCode> commodityBarCodeParamByTemplateCodeMap = 
			new HashMap<String, CommodityBarCodeParamByTemplateCode>();
	
	public static Map<String,BoxBarCodeParamByTemplateCode> boxBarCodeParamByTemplateCodeMap = 
			new HashMap<String, BoxBarCodeParamByTemplateCode>();
	public static final Integer BAR_CODE_NUM_COUNT = 1000;
	/**
	   * 生成条码模板，供单元测试使用
	   * @param template
	   * @param barCodeList
	   */
	 public static void createTemplate(BarCodePDFTemplate template,List<CommodityBarCode<String>> barCodeList,String localPath){
		  //float M = 13.5f*template.getRow();
		  CommodityBarCodeParamByTemplateCode param = getBarCodeParamByTemplateCodeByCode(template.getCode()); 
	 	  Document document = CommodityBarCodeParamByTemplateCode.getTemplateBorder(template.getCode());
	 	  //支持中文
	 	  PdfPTable mainTable = new PdfPTable(template.getColumn());   
	 	  mainTable.setTotalWidth(param.get_table_width());
	 	  mainTable.setLockedWidth(true);
	 	  //默认无边框
	 	  mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
	 	  mainTable.getDefaultCell().setPadding(0f);    // 设置此值是为了table内部不会产生多余的空隙
	 	  //表格居中对齐
	 	  mainTable.setHorizontalAlignment(Element.ALIGN_CENTER);
	 	  int total = 0;
	      try { 
	    	  //float h = (PageSize.A4.getHeight()- M)/template.getRow() -24; //条码高度，不算文字  文字设置为10像素固定
		      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(localPath)); 
		      document.open(); 
		      PdfContentByte cb = writer.getDirectContent();
		      
		      int loopCount = 0;
		      for(CommodityBarCode<String> barCode:barCodeList){
			      PdfPCell barcodeCell = createCommdityBarCodePartPDFCell(barCode, param, cb);
			      for(int j=0;j<barCode.getNum();j++){
			    	  loopCount += 1;
			    	  setMainTabelPadding(loopCount, template, mainTable);// 设置间距
			    	  mainTable.addCell(buildBarTable(template,barcodeCell,barCode,param));
			      }
			      total = barCode.getNum()+total;
		      }
		      int mergeInt = 0;
		      if(total%template.getColumn() != 0)
		    	  mergeInt = template.getColumn()-total%template.getColumn();
		      for(int i = 0;i<mergeInt;i++){
		    	  PdfPCell cell = new PdfPCell();
		    	  cell.setBorder(PdfPCell.NO_BORDER);
		    	  mainTable.addCell(cell);
		      }
		      
		      document.add(mainTable); 
		      document.close();
	      } catch (Exception de) {       
	    	  de.printStackTrace(); 
	      }
	 }
	 
	 /**
	   * 生成条码模板，供单元测试使用
	   * @param template
	   * @param barCodeList
	   */
	 public static void createBoxTemplate(BarCodePDFTemplate template,List<BoxBarCode> barCodeList,String localPath){
		  //float M = 13.71f*template.getRow();
		  BoxBarCodeParamByTemplateCode param = getBoxBarCodeParamByTemplateCode(template.getCode());
	 	  Document document = BoxBarCodeParamByTemplateCode.getTemplateBorder(template.getCode());
	 	  //支持中文
	 	  PdfPTable mainTable = new PdfPTable(template.getColumn());   
	 	  mainTable.setTotalWidth(param.get_table_width());
	 	  mainTable.setLockedWidth(true);
	 	  //默认无边框
	 	  mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
	 	  //表格居中对齐
	 	  mainTable.setHorizontalAlignment(Element.ALIGN_CENTER);
	 	  int total = 0;
	      try { 
	    	  //float h = ((PageSize.A4.getHeight()- M)/template.getRow() -24)/4; //条码高度，不算文字  文字设置为10像素固定
		      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(localPath)); 
		      document.open(); 
		      PdfContentByte cb = writer.getDirectContent();
		      
		      for(BoxBarCode barCode:barCodeList){
		    	  
		    	  PdfPTable bartable = createBoxBarCodePartPDFTable(template.getCode(), cb, barCode);
			      
		    	  for(int j=0;j<barCode.getNum();j++){
			    	  mainTable.addCell(buildBoxBarTable(bartable,barCode,template.getCode()));
			      }
			      total = barCode.getNum()+total;
		      }
		      int mergeInt = 0;
		      if(total%template.getColumn() != 0)
		    	  mergeInt = template.getColumn()-total%template.getColumn();
		      for(int i = 0;i<mergeInt;i++){
		    	  PdfPCell cell = new PdfPCell();
		    	  cell.setBorder(PdfPCell.NO_BORDER);
		    	  mainTable.addCell(cell);
		      }
		      
		      document.add(mainTable); 
		      document.close();
	      } catch (Exception de) {       
	    	  de.printStackTrace(); 
	      }
	 }
	 
	 /**
	   * 生成条码模板，供单元测试使用
	   * @param template
	   * @param barCodeList
	   */
	 public static void createBoxTemplate(BarCodePDFTemplate template,List<BoxBarCode> barCodeList,OutputStream os){
		  //float M = 13.71f*template.getRow();
		  BoxBarCodeParamByTemplateCode param = getBoxBarCodeParamByTemplateCode(template.getCode());
	 	  Document document = BoxBarCodeParamByTemplateCode.getTemplateBorder(template.getCode());
	 	  //支持中文
	 	  PdfPTable mainTable = new PdfPTable(template.getColumn());   
	 	  mainTable.setTotalWidth(param.get_table_width());
	 	  mainTable.setLockedWidth(true);
	 	  //默认无边框
	 	  mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
	 	  //表格居中对齐
	 	  mainTable.setHorizontalAlignment(Element.ALIGN_CENTER);
	 	  int total = 0;
	      try { 
	    	  //float h = ((PageSize.A4.getHeight()- M)/template.getRow() -24)/4; //条码高度，不算文字  文字设置为10像素固定
		      PdfWriter writer = PdfWriter.getInstance(document, os); 
		      document.open(); 
		      PdfContentByte cb = writer.getDirectContent();  
		      for(BoxBarCode barCode:barCodeList){
		    	  
		    	  PdfPTable bartable = createBoxBarCodePartPDFTable(template.getCode(), cb, barCode);
			      
		    	  for(int j=0;j<barCode.getNum();j++){
			    	  mainTable.addCell(buildBoxBarTable(bartable,barCode,template.getCode()));
			      }
			      total = barCode.getNum()+total;
		      }
		      int mergeInt = 0;
		      if(total%template.getColumn() != 0)
		    	  mergeInt = template.getColumn()-total%template.getColumn();
		      for(int i = 0;i<mergeInt;i++){
		    	  PdfPCell cell = new PdfPCell();
		    	  cell.setBorder(PdfPCell.NO_BORDER);
		    	  mainTable.addCell(cell);
		      }
		      
		      document.add(mainTable); 
		      document.close();
	      } catch (Exception de) {       
	    	  de.printStackTrace(); 
	      }
	 }
	 /**
	   * 通用条码模板，可供生成各种条码
	   * @param template
	   * @param barCodeList
	   * @param out
	   * @throws Exception
	   */
	 public static void createTemplate(BarCodePDFTemplate template,List<CommodityBarCode> barCodeList,OutputStream out) throws Exception{
		  //float M = 13.71f*template.getRow();
		  CommodityBarCodeParamByTemplateCode param = getBarCodeParamByTemplateCodeByCode(template.getCode());
	 	  Document document = CommodityBarCodeParamByTemplateCode.getTemplateBorder(template.getCode());
	 	  //支持中文
	 	  PdfPTable mainTable = new PdfPTable(template.getColumn());   
	 	  mainTable.setTotalWidth(param.get_table_width());
	 	  mainTable.setLockedWidth(true);
	 	  //默认无边框
	 	  mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
	 	  mainTable.getDefaultCell().setPadding(0f);    // 设置此值是为了table内部不会产生多余的空隙
	 	  //表格居中对齐
	 	  mainTable.setHorizontalAlignment(Element.ALIGN_CENTER);
    	  //float h = getBarCodeHeight(template.getCode());
    			  //(PageSize.A4.getHeight()- M)/template.getRow() -24; //条码高度，不算文字  文字设置为10像素固定
	      PdfWriter writer = PdfWriter.getInstance(document, out); 
	      document.open(); 
	      PdfContentByte cb = writer.getDirectContent();  
	      int total = 0;
	      int loopCount = 0;
	      for(CommodityBarCode<String> barCode:barCodeList){
	    	  
	    	  PdfPCell barcodeCell = createCommdityBarCodePartPDFCell(barCode, param, cb);
		      
	    	  for(int j=0;j<barCode.getNum();j++){
		    	  loopCount += 1;
		    	  setMainTabelPadding(loopCount, template, mainTable);// 设置间距
		    	  mainTable.addCell(buildBarTable(template,barcodeCell,barCode,param));
		      }
		      total = barCode.getNum()+total;
	      }
	      int mergeInt = 0;
	      if(total%template.getColumn() != 0)
	    	  mergeInt = template.getColumn()-total%template.getColumn();
	      for(int i = 0;i<mergeInt;i++){
	    	  PdfPCell cell = new PdfPCell();
	    	  cell.setBorder(PdfPCell.NO_BORDER);
	    	  mainTable.addCell(cell);
	      }
	      document.add(mainTable); 
	      document.close();
	 }
	 /**
	  * 生成箱子条码的PdfPTable
	  * @param barcodeCell
	  * @param barCode
	  * @param template_code
	  * @return
	  * @throws DocumentException
	  * @throws IOException
	  */
	 private static PdfPTable buildBoxBarTable(PdfPTable barcodeCell,BoxBarCode barCode,String template_code) throws DocumentException, IOException{
		 PdfPTable bartable   = new PdfPTable(1);   // 主表
		 bartable.getDefaultCell().setBorder(PdfPCell.NO_BORDER	);
		 bartable.getDefaultCell().setPadding(0f);  // 设置cell 之间没有间隙
		 
		 
		 /**从上到下第一部分(第一跳线之上的部分)   title  start*/
		 PdfPTable titleTable = new PdfPTable(2);
		 Paragraph titleLeft  = new Paragraph(barCode.getTitle(),BoxBarCodeParamByTemplateCode.getFirstPartFontByCode(template_code));
		 Paragraph titleRight = new Paragraph(barCode.getOwnerName(),BoxBarCodeParamByTemplateCode.getFirstPartFontByCode(template_code));
		 PdfPCell leftGcell   = BoxBarCodeParamByTemplateCode.createPdfCellFirstPart(titleLeft,Element.ALIGN_LEFT,false,template_code);
		 PdfPCell rightGcell  = BoxBarCodeParamByTemplateCode.createPdfCellFirstPart(titleRight,Element.ALIGN_RIGHT,false,template_code);
		 titleTable.addCell(leftGcell); 
		 titleTable.addCell(rightGcell);
  	  	 bartable.addCell(titleTable);   
  	  	 /**从上到下第一部分 (第一跳线之上的部分)   title  end*/
  	  	 
  	  	 
  	  	 /**从上到下第二部分(第二跳线之上第一条线以下的部分)   title  start*/
  	     PdfPTable msgTable  = new PdfPTable(2);  
  	     msgTable.setWidths(BoxBarCodeParamByTemplateCode.getSencodPdfPTabelCellProportion(template_code));
  	     Paragraph consignee = new Paragraph("Consignee:",BoxBarCodeParamByTemplateCode.getSecondPartLeftFontByCode(template_code));
  	     Paragraph add 		 = new Paragraph("ADD:",BoxBarCodeParamByTemplateCode.getSecondPartLeftFontByCode(template_code));
  	     Paragraph tel 		 = new Paragraph("TEL:",BoxBarCodeParamByTemplateCode.getSecondPartLeftFontByCode(template_code));
  	     Paragraph attn 	 = new Paragraph("ATTN:",BoxBarCodeParamByTemplateCode.getSecondPartLeftFontByCode(template_code));
  	     
  	     Paragraph consigneeInfo = new Paragraph(barCode.getConsignee(),BoxBarCodeParamByTemplateCode.getSecondPartRigthFontByCode(template_code));
	     Paragraph addInfo 		 = new Paragraph(barCode.getAdd(),BoxBarCodeParamByTemplateCode.getSecondPartRigthFontByCode(template_code));
	     Paragraph telInfo 		 = new Paragraph(barCode.getTel(),BoxBarCodeParamByTemplateCode.getSecondPartRigthFontByCode(template_code));
	     Paragraph attnInfo 	 = new Paragraph(barCode.getAttn(),BoxBarCodeParamByTemplateCode.getSecondPartRigthFontByCode(template_code));
  	     
  	     PdfPCell consigneeGcell =	BoxBarCodeParamByTemplateCode.createPdfCellSecondPartFirstRowLeft(consignee,Element.ALIGN_RIGHT,true,template_code);
	  	 PdfPCell addGcell 		 =  BoxBarCodeParamByTemplateCode.createPdfCellSecondPartMiddleRowLeft(add,Element.ALIGN_RIGHT,true,template_code);
	  	 PdfPCell telGcell 		 =  BoxBarCodeParamByTemplateCode.createPdfCellSecondPartMiddleRowLeft(tel,Element.ALIGN_RIGHT,true,template_code);
	  	 PdfPCell attnGcell 	 = 	BoxBarCodeParamByTemplateCode.createPdfCellSecondPartEndRowLeft(attn,Element.ALIGN_RIGHT,false,template_code);
	  	 
	  	 PdfPCell consigneeInfoGcell =	BoxBarCodeParamByTemplateCode.createPdfCellSecondPartFirstRowRight(consigneeInfo,Element.ALIGN_LEFT,true,template_code);
	  	 PdfPCell addInfoGcell 		 =  BoxBarCodeParamByTemplateCode.createPdfCellSecondPartMiddleRowRight(addInfo,Element.ALIGN_LEFT,true,template_code);
	  	 PdfPCell telInfoGcell 		 =  BoxBarCodeParamByTemplateCode.createPdfCellSecondPartMiddleRowRight(telInfo,Element.ALIGN_LEFT,true,template_code);
	  	 PdfPCell attnInfoGcell 	 = 	BoxBarCodeParamByTemplateCode.createPdfCellSecondPartEndRowRight(attnInfo,Element.ALIGN_LEFT,false,template_code);
	     
	     msgTable.addCell(consigneeGcell);
	     msgTable.addCell(consigneeInfoGcell);
	     msgTable.addCell(addGcell);
	     msgTable.addCell(addInfoGcell);
	     msgTable.addCell(telGcell);
	     msgTable.addCell(telInfoGcell);
	     msgTable.addCell(attnGcell); 
	     msgTable.addCell(attnInfoGcell); 
  	     
  	  	 bartable.addCell(msgTable);//发货、收货信息
  	  	 /**从上到下第二部分(第二跳线之上第一条线以下的部分)   title  end*/
  	  	 
  	  	 
  	  	 /**从上到下第三部分(第三跳线之上第二条线以下的部分)   title  start*/
		 bartable.addCell(barcodeCell); //Receipt ID,条码，条码对应的编码信息
		 /**从上到下第三部分(第三跳线之上第二条线以下的部分)   title  end*/
		 
		 
		 /**从上到下第四部分(第三跳线以下的部分)   title  start*/
		 PdfPTable bottmTable  = new PdfPTable(4);  
		 bottmTable.setWidths(BoxBarCodeParamByTemplateCode.getEndPdfFTableCellProportion(template_code));
  	     Paragraph planId      = new Paragraph("Plan ID:",BoxBarCodeParamByTemplateCode.getEndPartTitleFontByCode(template_code));
  	     Paragraph planIdInfo  = new Paragraph(barCode.getPlanId(),BoxBarCodeParamByTemplateCode.getEndPartTextFontByCode(template_code));
  	     PdfPCell  planIdGcell = BoxBarCodeParamByTemplateCode.createPdfCellEndPartFirstCell(planId,Element.ALIGN_LEFT,true,template_code);
  	     PdfPCell  planIdInfoGcell = BoxBarCodeParamByTemplateCode.createPdfCellEndPartSencodCell(planIdInfo,Element.ALIGN_CENTER,true,template_code);
	     
  	     Paragraph carton      = new Paragraph("Carton:",BoxBarCodeParamByTemplateCode.getEndPartTitleFontByCode(template_code));
	     Paragraph cartonInfo  = new Paragraph(barCode.getCarton(),BoxBarCodeParamByTemplateCode.getFourPartFontByCode(template_code));
	     PdfPCell  cartonGcell = BoxBarCodeParamByTemplateCode.createPdfCellEndPartThridCell(carton,Element.ALIGN_RIGHT,true,template_code);
	     PdfPCell  cartonInfoGcell = BoxBarCodeParamByTemplateCode.createPdfCellEndPartEndCell(cartonInfo,Element.ALIGN_RIGHT,true,template_code);
  	     
	     bottmTable.addCell(planIdGcell);
	     bottmTable.addCell(planIdInfoGcell);
	     bottmTable.addCell(cartonGcell);
	     bottmTable.addCell(cartonInfoGcell);
	     bartable.addCell(bottmTable);
	     /**从上到下第四部分(第三跳线以下的部分)   title  end*/
	     
		 return bartable;
	 }
	 
	 /**
	  * 箱子条码      生成装Receipt ＩＤ 和条码的PdfPTable
	  * @param code
	  * @param cb
	  * @param barCode
	  * @return
	  */
	 private static PdfPTable createBoxBarCodePartPDFTable(String code,PdfContentByte cb,BoxBarCode barCode)
     {
		Barcode128 code128 = new Barcode128();
		code128.setCode(barCode.getCode());
		String fullcode = code128.getRawText(barCode.getCode(), false);
		code128.setX(BoxBarCodeParamByTemplateCode.getBarCodeWidthSizeByCode(code)/ ((fullcode.length() + 2) * 11 + 2f)); // 设置条码的宽度
		code128.setBarHeight(BoxBarCodeParamByTemplateCode.getBarCodeHeight(code)); // 设置条码的高度
		code128.setSize(BoxBarCodeParamByTemplateCode.getBarCodeFontSize(code));    // 此处设置此值是无效的，因为setFont(null),将不显示条码下方的编码
		code128.setFont(null);
		Image image128 = code128.createImageWithBarcode(cb, null, null);
		PdfPTable bartable = new PdfPTable(1);
		// 设置Receipt ID字体
		Paragraph graph = new Paragraph("Receipt ID",BoxBarCodeParamByTemplateCode.getBorCodePartFontByTemplateCode(code));
		// 设置code字体
		Paragraph bar_code = new Paragraph(barCode.getCode(),BoxBarCodeParamByTemplateCode.getBorCodePartFontByTemplateCode(code));
		PdfPCell codeCell = BoxBarCodeParamByTemplateCode.createPdfCellBarCode(bar_code, Element.ALIGN_CENTER, false,code);
		PdfPCell bartitlecell = BoxBarCodeParamByTemplateCode.createPdfCellReceiptId(graph, Element.ALIGN_CENTER, true,code);
		PdfPCell barcodeCell = new PdfPCell(image128);
		barcodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		barcodeCell.setBorder(PdfPCell.NO_BORDER);
		bartable.addCell(bartitlecell);
		bartable.addCell(barcodeCell);
		bartable.addCell(codeCell);
		return bartable;
	 }
	 
	 /**
	  * 生成商品条码的PdfPCell
	  * @param barCode
	  * @param param
	  * @param cb
	  * @return
	  */
	 private static PdfPCell createCommdityBarCodePartPDFCell(CommodityBarCode barCode,CommodityBarCodeParamByTemplateCode param,PdfContentByte cb)
	 {
		  Barcode128 code128 = new Barcode128();       
	      code128.setCode(barCode.getCode());
	      String fullcode = code128.getRawText(barCode.getCode(), false);
	      code128.setX(param.get_bar_code_width() / ((fullcode.length() + 2) * 11 + 2f)); //设置条码的宽度
	      code128.setBarHeight(param.get_bar_code_height());                              //设置条码的高度
	      code128.setFont(null);                                                          //设置条码下方的text不显示
	      Image image128 = code128.createImageWithBarcode(cb, null, null);  
	      PdfPCell barcodeCell = new PdfPCell(image128);
	      barcodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
	      barcodeCell.setPaddingTop(param.get_bar_code_padding_top_size());               //设置条码与上边界的距离
	      barcodeCell.setBorder(PdfPCell.NO_BORDER);
	      return barcodeCell;
	 }
	 /**
	  * 生成商品条码的PdfPTable
	  * @param barcodeCell
	  * @param barCode
	  * @param param
	  * @return
	  * @throws DocumentException
	  * @throws IOException
	  */
	 private static PdfPTable buildBarTable(BarCodePDFTemplate template,PdfPCell barcodeCell,CommodityBarCode<String> barCode,CommodityBarCodeParamByTemplateCode param ) throws DocumentException, IOException{
		BaseFont bfChinese_8 = BaseFont.createFont("STSong-Light",
				"UniGB-UCS2-H", false);
		Font fontsize8 = new Font(bfChinese_8, 8, Font.NORMAL); // text 字体
		BaseFont bfChinese_11 = BaseFont.createFont(BaseFont.HELVETICA,
		          BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
		Font fontsize11 = new Font(bfChinese_11, 11, Font.NORMAL); // 条码下方文字字体
		PdfPTable bartable = new PdfPTable(1);
		barcodeCell.setFixedHeight(param.get_bar_code_cell_fixed_hight()); // 设置条码所在cell的固定高度
		// 添加条码
		bartable.addCell(barcodeCell);
		// 添加条码下方的Text start
		Paragraph bar_code = new Paragraph(barCode.getCode(), fontsize11);
		PdfPCell codeCell = new PdfPCell(bar_code);
		codeCell.setVerticalAlignment(Element.ALIGN_TOP);
		codeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		codeCell.setUseAscender(true);
		codeCell.setUseDescender(true);
		codeCell.setBorder(PdfPCell.NO_BORDER);
		codeCell.setPaddingTop(3f);
		bartable.addCell(codeCell);
		// 添加文本Text
		Paragraph graph = new Paragraph(CommodityBarCodeParamByTemplateCode.getTitleByCode(template.getCode(), barCode.getTitle()), fontsize8);
		PdfPCell gcell = new PdfPCell(graph);
		gcell.setHorizontalAlignment(Element.ALIGN_CENTER);
		gcell.setVerticalAlignment(Element.ALIGN_CENTER);
		gcell.setUseAscender(true);
		gcell.setUseDescender(true);
		gcell.setBorder(PdfPCell.NO_BORDER);
		gcell.setPaddingTop(param.get_text_padding_top_size());       // 设置文本与顶部的值
		gcell.setPaddingBottom(param.get_text_padding_bottom_size()); // 设置文本与底部的的距离
		bartable.addCell(gcell);
		bartable.setHorizontalAlignment(Element.ALIGN_CENTER);
		return bartable;
	 }
	 
	 /**
	  * 设置每页最后一行不再有空隙，每行最后一列不再有空隙
	  * @param loopCount
	  * @param template
	  * @param mainTable
	  */
	 public static void setMainTabelPadding(int loopCount,BarCodePDFTemplate template,PdfPTable mainTable)
	 {
		 if(CommodityBarCodeParamByTemplateCode.getPaddingBottomByCode(template.getCode()) > 0)
		 {
			 int pageTdCount = template.getRow()*template.getColumn();
		     int currageLoopCount = loopCount % pageTdCount;
		     int flagCount = pageTdCount - currageLoopCount;
		     if(flagCount < 4 || currageLoopCount == 0)
		    	 mainTable.getDefaultCell().setPaddingBottom(0f); // 最后一行设置上下间距为0 
		     else
		    	 mainTable.getDefaultCell().setPaddingBottom(CommodityBarCodeParamByTemplateCode.getPaddingBottomByCode(template.getCode())); // 设置上下间距
		 }
		 /** 不能控制水平方向cell之间的距离，只能均分，之后再做处理
		  * else if(CommodityBarCodeParamByTemplateCode.getPaddingRightByCode(template.getCode()) > 0)
		 {
			 int flagCount = loopCount % template.getColumn();
			 if(flagCount == 0)
				 mainTable.getDefaultCell().setPaddingRight(0f); // 最后一列设置右间距为0 
			 else
				 mainTable.getDefaultCell().setPaddingRight(CommodityBarCodeParamByTemplateCode.getPaddingRightByCode(template.getCode())); // 非最后一列设置右间距 
		 }*/ 
	 }
	 
	 public static void main(String[] args) throws DocumentException, IOException {
		 /*BarCodePDFTemplate template = getBoxBarCodeTemplateList().get(1);
		 List<BoxBarCode> list = new ArrayList<BoxBarCode>();
		 BoxBarCode  param = new BoxBarCode();
		 param.setAdd("123 N Sunset Ave, City of Industry,CA91744,US");
		 param.setCode("2020165M1000197");
		 param.setTitle("XBN3");
		 param.setAttn("American-Chain E-CommerceCo.LTD");
		 param.setConsignee("德邦快递");
		 param.setTel("6263204831");
		 param.setOwnerName("BJHSRT103");
		 param.setPlanId("123456789012345");
		 param.setCarton("9999/502");
		 param.setParam("XBN01");
		 param.setNum(1);
		 list.add(param);
		 String localPath = "f:/pdf/20160712/9"+File.separator+UUID.randomUUID().toString()+".pdf"; 
		 createBoxTemplate(template,list,localPath);*/
		/** Document document = new Document();
         // step 2
         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( "E:/pdf"+File.separator+UUID.randomUUID().toString()+".pdf"));
         // step 3
         document.open();
         // step 4
         XMLWorkerHelper.getInstance().parseXHtml(writer, document, BarCodeUtil.class.getResourceAsStream("index.html"));
         //step 5
         document.close();
         System.out.println( "PDF Created!" );*/
		 BarCodePDFTemplate template = getPDFTemplateByCode(0);
		 List<CommodityBarCode<String>> list = new ArrayList<CommodityBarCode<String>>();
		 CommodityBarCode<String> orderCode = new CommodityBarCode<>();
		 orderCode.setCode("EB201601130001");
		 orderCode.setTitle("出库单号");
		 orderCode.setNum(1);
		 list.add(orderCode);
		/* CommodityBarCode<String> commodity = new CommodityBarCode<>();
		 commodity.setCode("BG150003");
		 commodity.setTitle("货品条码");
		 commodity.setNum(4);
		 list.add(commodity);
		 CommodityBarCode<String> userName = new CommodityBarCode<>();
		 userName.setCode("xbn01");
		 userName.setTitle("用户名");
		 userName.setNum(4);
		 list.add(userName);
		 CommodityBarCode<String> pwd = new CommodityBarCode<>();
		 pwd.setCode("123");
		 pwd.setTitle("密码");
		 pwd.setNum(8);
		 list.add(pwd);
		 CommodityBarCode<String> hw = new CommodityBarCode<>();
		 hw.setCode("1-01-01-12");
		 hw.setTitle("货位");
		 hw.setNum(8);
		 list.add(hw);
		 CommodityBarCode<String> xxtm = new CommodityBarCode<>();
		 xxtm.setCode("112-112-112");
		 xxtm.setTitle("箱子型号");
		 xxtm.setNum(8);
		 list.add(xxtm);
		 CommodityBarCode<String> lctm = new CommodityBarCode<>();
		 lctm.setCode("LC20160001");
		 lctm.setTitle("笼车条码");
		 lctm.setNum(8);
		 list.add(lctm);*/
		 String localPath = "f:/pdf/20160715/0"+File.separator+UUID.randomUUID().toString()+".pdf"; 
		 createTemplate(template,list,localPath);
	}
	
	public static List<BarCodePDFTemplate> getCommodityBarCodeTemplateList(){
		if(barCodeTemplate.size()>0)
			return barCodeTemplate;
		else{
			BarCodePDFTemplate bcp1 = new BarCodePDFTemplate("1",7 ,3,"A4纸上最多可打印 21 枚 63.8mm X 38.1mm 的直角标签");
			BarCodePDFTemplate bcp2 = new BarCodePDFTemplate("2",8 ,3,"A4纸上最多可打印 24 枚 64.5mm X 33.8mm 的直角标签");
			BarCodePDFTemplate bcp3 = new BarCodePDFTemplate("3",8 ,3,"A4纸上最多可打印 24 枚 70.0mm X 37.1mm 的直角标签");
			BarCodePDFTemplate bcp4 = new BarCodePDFTemplate("4",10,4,"A4纸上最多可打印 40 枚 52.5mm X 29.7mm 的直角标签");
			BarCodePDFTemplate bcp5 = new BarCodePDFTemplate("5",11,4,"A4纸上最多可打印 44 枚 48.5mm X 25.4mm 的直角标签");
			BarCodePDFTemplate bcp6 = new BarCodePDFTemplate("6",9 ,4,"A4纸上最多可打印 36 枚 50.0mm X 30.0mm 的圆角标签");
			BarCodePDFTemplate bcp7 = new BarCodePDFTemplate("7",7 ,3,"A4纸上最多可打印 21 枚 60.0mm X 40.0mm 的圆角标签");
			BarCodePDFTemplate bcp8 = new BarCodePDFTemplate("0",1 ,1,"A8纸上打印 1 枚 标签");
			barCodeTemplate.add(bcp1);
			barCodeTemplate.add(bcp2);
			barCodeTemplate.add(bcp3);
			barCodeTemplate.add(bcp4);
			barCodeTemplate.add(bcp5);
			barCodeTemplate.add(bcp6);
			barCodeTemplate.add(bcp7);
			barCodeTemplate.add(bcp8);
			return barCodeTemplate;
		}
	}
	
	public static List<BarCodePDFTemplate> getBoxBarCodeTemplateList(){
		if(commodityBarCodeTemplate.size()>0)
			return commodityBarCodeTemplate;
		else{
			BarCodePDFTemplate bcp8 = new BarCodePDFTemplate("8",3 ,2,"A4纸上最多可打印 6 枚 95.0mm X 95.0mm 的圆角标签");
			BarCodePDFTemplate bcp9 = new BarCodePDFTemplate("9",2 ,2,"A4纸上最多可打印 4枚 102.0mm X 146.0mm 的圆角标签");
			commodityBarCodeTemplate.add(bcp8);
			commodityBarCodeTemplate.add(bcp9);
			return commodityBarCodeTemplate;
		}
	}
	public static BarCodePDFTemplate getPDFTemplateByCode(int code){
		if(barCodeTemplate.size()<1){
			getCommodityBarCodeTemplateList();
		}
		if(commodityBarCodeTemplate.size()<1){
			getBoxBarCodeTemplateList();
		}
		if(code <8)
		{
			if(code == 0)
				return barCodeTemplate.get(7);
			return barCodeTemplate.get(code-1);
		}
		else 
			return commodityBarCodeTemplate.get(code-8);
			
	}
	public static void setCommodityBarCodeParamByTemplateCodeMap()
	{
		if(commodityBarCodeParamByTemplateCodeMap.size() < 1)
		{
			CommodityBarCodeParamByTemplateCode b_1 = new CommodityBarCodeParamByTemplateCode("1");
			CommodityBarCodeParamByTemplateCode b_2 = new CommodityBarCodeParamByTemplateCode("2");
			CommodityBarCodeParamByTemplateCode b_3 = new CommodityBarCodeParamByTemplateCode("3");
			CommodityBarCodeParamByTemplateCode b_4 = new CommodityBarCodeParamByTemplateCode("4");
			CommodityBarCodeParamByTemplateCode b_5 = new CommodityBarCodeParamByTemplateCode("5");
			CommodityBarCodeParamByTemplateCode b_6 = new CommodityBarCodeParamByTemplateCode("6");
			CommodityBarCodeParamByTemplateCode b_7 = new CommodityBarCodeParamByTemplateCode("7");
			CommodityBarCodeParamByTemplateCode b_8 = new CommodityBarCodeParamByTemplateCode("8");
			
			commodityBarCodeParamByTemplateCodeMap.put("1", b_1);
			commodityBarCodeParamByTemplateCodeMap.put("2", b_2);
			commodityBarCodeParamByTemplateCodeMap.put("3", b_3);
			commodityBarCodeParamByTemplateCodeMap.put("4", b_4);
			commodityBarCodeParamByTemplateCodeMap.put("5", b_5);
			commodityBarCodeParamByTemplateCodeMap.put("6", b_6);
			commodityBarCodeParamByTemplateCodeMap.put("7", b_7);
			commodityBarCodeParamByTemplateCodeMap.put("8", b_8);
		}
	}
	public static CommodityBarCodeParamByTemplateCode getBarCodeParamByTemplateCodeByCode(String code)
	{
		if(commodityBarCodeParamByTemplateCodeMap.size() < 1)
			setCommodityBarCodeParamByTemplateCodeMap();
		return commodityBarCodeParamByTemplateCodeMap.get(code);
	}
	public static void setBoxBarCodeParamByTemplateCodeMap()
	{
		if(boxBarCodeParamByTemplateCodeMap.size() < 1)
		{
			BoxBarCodeParamByTemplateCode b_8 = new BoxBarCodeParamByTemplateCode("8");
			BoxBarCodeParamByTemplateCode b_9 = new BoxBarCodeParamByTemplateCode("9");
			
			boxBarCodeParamByTemplateCodeMap.put("8", b_8);
			boxBarCodeParamByTemplateCodeMap.put("9", b_9);
		}
	}
	public static BoxBarCodeParamByTemplateCode getBoxBarCodeParamByTemplateCode(String code)
	{
		if(boxBarCodeParamByTemplateCodeMap.size() < 1)
			setBoxBarCodeParamByTemplateCodeMap();
		return boxBarCodeParamByTemplateCodeMap.get(code);
	}
	
	/**
	 * 检测要打印的条码数不能大于1000
	 * @param boxBarCodes
	 * @throws SOAServiceException
	 */
	public static  void checkBoxCodeNumCount(List<BoxBarCode > boxBarCodes) throws SOAServiceException
	{
		long countNum = 0;
		for(BoxBarCode bbc : boxBarCodes)
			countNum += bbc.getNum();
		if(countNum > BarCodeUtil.BAR_CODE_NUM_COUNT)
			throw new SOAServiceException();
	}
	
	/**
	 * 检测打印的货品条码总不能大于1000个
	 * @param barCodeList
	 * @throws SOAServiceException
	 */
	public static  void checkCommodityCodeNumCount(List<CommodityBarCode<String>> barCodeList)throws SOAServiceException
	{
		long numCount = 0;
		for(CommodityBarCode<String> cbc : barCodeList)
			numCount += cbc.getNum();
		if(numCount > BarCodeUtil.BAR_CODE_NUM_COUNT)
			throw new SOAServiceException();
	}
	
	/**
	 * 检测打印的货品条码总不能大于1000个
	 * @param barCodeList
	 * @throws SOAServiceException
	 */
	public static  void checkCommodityCodeNumCount_(List<CommodityBarCode> barCodeList)throws SOAServiceException
	{
		long numCount = 0;
		for(CommodityBarCode cbc : barCodeList)
			numCount += cbc.getNum();
		if(numCount > BarCodeUtil.BAR_CODE_NUM_COUNT)
			throw new SOAServiceException();
	}
}
